package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前菜品或套餐是否在购物车中（查询shopping_cart表，根据dishId或setmealId查询）
        ShoppingCart sct = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, sct);
        sct.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByDishIdOrSetmealId(sct);
        // 如果已经存在，只需将数量加一
        if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
            log.info("购物车中已经存在该菜品或套餐，数量加一");
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            // 如果不存在，则插入一条购物车，数量默认为1
            log.info("购物车中不存在该菜品或套餐，插入一条购物车记录");
            //判断是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
                //传来的是菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                sct.setName(dish.getName());
                sct.setImage(dish.getImage());
                sct.setAmount(dish.getPrice());
            } else {
                //传来的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                sct.setName(setmeal.getName());
                sct.setImage(setmeal.getImage());
                sct.setAmount(setmeal.getPrice());
            }
            sct.setNumber(1);
            sct.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(sct);
        }
    }
    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.selectAll(userId);
    }
    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }
    /**
     * 删除购物车中一个商品
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //查询当前商品在购物车中的数量
        ShoppingCart sct = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, sct);
        sct.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByDishIdOrSetmealId(sct);
        if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
            ShoppingCart cart = shoppingCartList.get(0);
            if (cart.getNumber() == 1) {
                //如果数量为1，则从购物车中删除该商品
                shoppingCartMapper.deleteById(cart.getId());
            } else {
                //如果数量不为1，则将数量减一
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(cart);
            }
        }
    }
}
